<?php

namespace app\common\model;

use think\Model;

use function now_time;

/**
 * 分类
 *
 * @property bool $is_common 是否公共  0=否 1=是
 * @property bool $level 层级
 * @property bool $status 状态： 1 正常 2 删除
 * @property int $menu_id
 * @property int $pid 上级ID
 * @property int $sort 权重
 * @property string $app 应用名称
 * @property string $create_time 创建时间
 * @property string $menu_icon 图标
 * @property string $menu_name 菜单名称
 * @property string $menu_url 菜单url
 * @property string $p_menu_url
 * @property string $type 1菜单 2=按钮 3=API
 */

class AdminMenu extends BaseModel
{
    protected $resultSetType = 'collection';
    protected $autoWriteTimestamp = false;
    protected $pk ='menu_id';
    /**
     * 表名,
     */
    protected $table = 'admin_menu';

    public static function build()
    {
        return new self();
    }


    public function cms_add($json){
        $data['menu_name']=$json['menu_name'];
        if(!empty($json['menu_url'])){
            $data['menu_url']=$json['menu_url'];
        }


        $data['type']=$json['type'];
        $data['create_time']=now_time(time());
        $list=$this->insert($data);
        return $list;
    }
    public function cms_edit($menu_id,$json){
        $data['menu_name']=$json['menu_name'];
        if(!empty($json['menu_url'])){
            $data['menu_url']=$json['menu_url'];
        }


        $data['type']=$json['type'];

        $list=$this->where('menu_id',$menu_id)->update($data);
        return $list;
    }

    public function selectList(){
        //获取所有的一级和二级分类
        $where="type='1' or type='2'";
        $list=$this->where($where)->order('create_time')->select();
        return $list;

    }


    public static function getListByGroup($group_id){
        //超级管理员
        if($group_id ==1){
            $list = AdminMenu::build()->whereIn('status',1)
            ->whereIn('type',[1,2])
            ->column('menu_id');
        }else{
            $list = AdminAuthGroup::build()
                ->where('group_id' ,$group_id)
                ->value('menu_rules');
        }
        return $list;
    }



}